//
//  CASettingController.m
//  cascade-realm
//
//  Created by QSC on 24/5/24.
//

#import "CASettingController.h"

#import "CALoginController.h"
@interface CASettingController ()
@property (nonatomic,copy)NSString * translateKey;
@end

@implementation CASettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navTitle = @"Setting";
    self.translateKey = @"AutoTranslate";
    
    
    UIButton * copyBtn = [UIButton new];
    [copyBtn setImage:Img(@"setting_copy") forState:UIControlStateNormal];
    [copyBtn addTarget:self action:@selector(copyClick) forControlEvents:UIControlEventTouchUpInside];
    UIView *idView = [self settingRow:[NSString stringWithFormat:@"ID:%@",[CAUserInfo shared].userInfo.showID] rightView:copyBtn];
    
    [self.view addSubview:idView];
    [idView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(44);
        make.top.equalTo(self.navView.mas_bottom).offset(32);
    }];
    
    
    
    UISwitch * sw = [UISwitch new];
    sw.on = [[NSUserDefaults standardUserDefaults] boolForKey:self.translateKey];
   
    sw.tintColor = Hex(0x86FB8C);
    sw.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.8, 0.8);
    [sw addTarget:self action:@selector(swDidChange:) forControlEvents:UIControlEventValueChanged];
    UIView * translateView = [self settingRow:@"Auto Translate" rightView:sw];
    [self.view addSubview:translateView];
    [translateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(44);
        make.top.equalTo(idView.mas_bottom).offset(20);
    }];
    

    
    UIButton * logoutBtn = [UIButton new];
    [logoutBtn setImage:Img(@"setting_arrow") forState:UIControlStateNormal];
    logoutBtn.userInteractionEnabled = NO;
    
    UIView * logoutView = [self settingRow:@"Logout" rightView:logoutBtn];
    [self.view addSubview:logoutView];
    [logoutView  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(44);
        make.top.equalTo(translateView.mas_bottom).offset(20);
    }];
    UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(logoutClick)];
    [logoutView addGestureRecognizer:tap1];
    
    
    UIButton * deleteBtn = [UIButton new];
    [deleteBtn setImage:Img(@"setting_arrow") forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(deleteClick) forControlEvents:UIControlEventTouchUpInside];
    deleteBtn.userInteractionEnabled = NO;
    UIView * deleteView = [self settingRow:@"Delete account" rightView:deleteBtn];
    [self.view addSubview:deleteView];
    [deleteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(44);
        make.top.equalTo(logoutView .mas_bottom).offset(20);
    }];
    UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteClick)];
    [deleteView addGestureRecognizer:tap2];
}

- (void)copyClick{
    UIPasteboard * pad = [UIPasteboard generalPasteboard];
    pad.string = [CAUserInfo shared].userInfo.showID;
    [EasyTextView showText:@"Copied to the clipboard"];
}

- (void)logoutClick{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"Logout?" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action1];
    
    WeakSelf
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf LogoutAction];
        
    }];
    [alert addAction:action2];
    
    
    [self presentViewController:alert animated:YES completion:nil];
    
}


- (void)deleteClick{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"DeleteAccount?" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action1];
    
    WeakSelf
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
      
        [weakSelf deleteAction];
    
        
    }];
    [alert addAction:action2];
    
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)LogoutAction{
    [Http PostLogWithPage:@"logout"];
    [Http postWithURL:logoutUrl params:@{} success:^(id  _Nonnull responseObject) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
        [UIApplication sharedApplication].keyWindow.rootViewController = [CALoginController new];
        
    } failure:^(id  _Nonnull errObject) {
        
    }];
    
    
}

- (void)deleteAction{
    
    [Http PostLogWithPage:@"deleteaccount"];
    [Http postWithURL:deleteAccount params:@{} success:^(id  _Nonnull responseObject) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
        [UIApplication sharedApplication].keyWindow.rootViewController = [CALoginController new];
        
    } failure:^(id  _Nonnull errObject) {
        
    }];
}



- (void)swDidChange:(UISwitch *)sw{

    [[NSUserDefaults standardUserDefaults] setBool:sw.isOn forKey:self.translateKey];
}


- (UIView *)settingRow:(NSString *)leftTitle rightView:(UIView *)rightView{
    
    UIView * view = [UIView new];
    view.layer.cornerRadius = 22;
    view.backgroundColor = [UIColor whiteColor];
    [view setDefultBorder];
    
    UILabel * label = [UILabel LabelWithFont:Font(16) color:Hex(0x404040) text:leftTitle];
    
    [view addSubview:label];
    [view addSubview:rightView];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.left.equalTo(view).offset(20);
    }];
    
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.right.equalTo(view).offset(-20);
    }];
    
    return view;
}




@end
